/**
 * Project: apollo-base-tools
 * 
 * File Created at 2017年8月7日
 * 
 * Copyright 2015-2016 dx.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * DongXue software Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dx.com.
 */
package org.apollo.sqlite.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apollo.sys.SysLog;
import org.sqlite.SQLiteConfig;

/**
 * @ClassName: DBConfig
 * @Description: 数据库配置
 * @author wuzhenfang(wzfbj2008@163.com)
 * @date 2017年8月7日 上午12:25:27
 * @version V1.0
 */
public class DBConfig extends SQLiteConfig {

	private final static Logger Log = SysLog.getLogger(DBConfig.class);

	private String defaultDBName = "default.db";

	private Properties prop;

	private String databaseName;

	private String dbFilePath;

	private int databaseVersion;

	private String databaseURL;

	private String encoding;
	
	private boolean debug;

	public DBConfig(String dbFilePath) {
		init(dbFilePath);
		setProp(prop);
	}

	public DBConfig(Properties prop) {
		super(prop);
	}

	public void init(String dbFilePath) {
		this.dbFilePath = dbFilePath;
		prop = new Properties();
		URL url = ClassLoader.getSystemResource("database.properties");
		try {
			InputStreamReader in = new InputStreamReader(new FileInputStream(url.getPath()), "UTF-8");
			if (in != null) {
				prop.load(in);
			}
		} catch (UnsupportedEncodingException e) {
			Log.error("", e);
		} catch (FileNotFoundException e) {
			Log.error("", e);
		} catch (IOException e) {
			Log.error("", e);
		}
		this.databaseVersion = Integer.valueOf((String) prop.get("user_version"));
		this.databaseURL = String.valueOf(prop.get("url"));
		this.databaseName = databaseURL.split(":")[2];
		this.encoding = String.valueOf(prop.get("encoding"));
	}

	/**
	 * @return the encoding
	 */
	public String getEncoding() {
		return encoding;
	}

	/**
	 * @param encoding the encoding to set
	 */
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	/**
	 * @return the databaseName
	 */
	public String getDatabaseName() {
		return databaseName;
	}

	/**
	 * @param databaseName the databaseName to set
	 */
	public void setDatabaseName(String databaseName) {
		this.databaseName = databaseName;
	}

	/**
	 * @return the databaseVersion
	 */
	public int getDatabaseVersion() {
		return databaseVersion;
	}

	/**
	 * @param databaseVersion the databaseVersion to set
	 */
	public void setDatabaseVersion(int databaseVersion) {
		this.databaseVersion = databaseVersion;
	}

	/**
	 * @return the databaseURL
	 */
	public String getDatabaseURL() {
		return databaseURL;
	}

	/**
	 * @param databaseURL the databaseURL to set
	 */
	public void setDatabaseURL(String databaseURL) {
		this.databaseURL = databaseURL;
	}

	/**
	 * @return the prop
	 */
	public Properties getProp() {
		return prop;
	}

	/**
	 * @param prop the prop to set
	 */
	public void setProp(Properties prop) {
		this.prop = prop;
	}
}
